package meetingcostclock;

import java.text.NumberFormat;
import java.util.Locale;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;

public class MeetingCostClock extends Thread {
	
	private final NumberFormat NUMB_FMT;
	private final Color BLACK;
	private final Color GRAY;
	
	private int noOfParticipants;
    private int rate;

	private long startTime;
	private float lastValue = 0;
    private boolean running = false; 
    private Label label;
    private Display display;

	public MeetingCostClock(int noOfParticipants, int rate, Locale locale, boolean onTop) {
        this.noOfParticipants = noOfParticipants;
        this.rate = rate;
        
        NUMB_FMT = NumberFormat.getCurrencyInstance(locale);
        
        this.display = new Display();
        //Shell shell = new Shell(display, SWT.CLOSE|SWT.TITLE|SWT.TOOL);
		int style = SWT.CLOSE|SWT.TITLE|SWT.TOOL;
		if (onTop) style = SWT.ON_TOP|SWT.CLOSE|SWT.TITLE|SWT.TOOL;
		Shell shell = new Shell(this.display, style);
		GridLayout layout = new GridLayout(3, false);
		layout.verticalSpacing = 0;
		shell.setLayout(layout);
        shell.setSize(250, 105);
        shell.setText("Meeting Cost Clock");

        BLACK = new Color(display, 0,0,0);
        GRAY = new Color(display, 100,100,100);
        
		final Link btnStartStop = new Link(shell, SWT.NONE);
		btnStartStop.setText("<a>Start</a>");
	    btnStartStop.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (running) {
					stopClock();
					btnStartStop.setText("<a>Start</a>");
				} else {
					startClock();
					btnStartStop.setText("<a>Stop</a>");
				}
			}
		});

		Link btnReset = new Link(shell, SWT.NONE);
		btnReset.setText("<a>Reset</a>");
	    btnReset.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				resetClock();
			}
		});

		Link btnSettings = new Link(shell, SWT.NONE);
		btnSettings.setText("<a>Settings</a>");
		GridData gData = new GridData();
	    gData.horizontalAlignment = GridData.END;
	    btnSettings.setLayoutData(gData);
	    btnSettings.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				display.close();
				new SettingsDialog();
			}
		});
        
        this.label = new Label (shell, SWT.NONE);
        this.label.setAlignment(SWT.CENTER);
    	this.label.setForeground(GRAY);
        this.label.setText(NUMB_FMT.format(0));
        Font fnt = new Font(label.getFont().getDevice(), "Verdana", 35, 0);  
        this.label.setFont(fnt);
		gData = new GridData();
	    gData.horizontalAlignment = GridData.FILL;
	    gData.grabExcessHorizontalSpace = true;
	    gData.horizontalSpan = 3;
	    label.setLayoutData(gData);
        
        shell.open ();
        
        this.display.timerExec(0, this);
        
        while (!shell.isDisposed ()) {
            if (!this.display.readAndDispatch ()) this.display.sleep ();
        }
        display.dispose ();
	}

    public void run() {
    	if (running) {
    		updateLabel();
    	}
        this.display.timerExec(500, this);
    }
    
	private void updateLabel() {
    	long currentTime = System.currentTimeMillis();
    	float diffHours = ((float)(currentTime - startTime))/1000/60/60;
    	this.label.setForeground(BLACK);
        this.label.setText(NUMB_FMT.format(lastValue+diffHours*rate*noOfParticipants));
	}
    
    public void startClock() {
        this.startTime = System.currentTimeMillis();
        this.running = true;
        updateLabel();
    }
    
    public void stopClock() {
    	this.running = false;
    	this.label.setForeground(GRAY);
    	
    	long currentTime = System.currentTimeMillis();
    	float diffHours = ((float)(currentTime - startTime))/1000/60/60;
    	this.lastValue = lastValue+diffHours*rate*noOfParticipants;
    }

    public void resetClock() {
    	this.startTime = System.currentTimeMillis();
    	this.lastValue = 0;
    	this.label.setText(NUMB_FMT.format(0));
    }

}
